package com.colorlight.cloud.gateway.api.message.convert;

import com.colorlight.cloud.gateway.api.message.constraint.protocol.ProtocolType;
import com.colorlight.cloud.gateway.api.message.constraint.schedule.ScheduleActions;
import com.google.protobuf.ByteString;
import com.google.protobuf.MessageLite;
import com.iot.proto.gateway.Gateway;
import com.iot.proto.type.ProtocolSummationType;
import lombok.Data;
import lombok.NonNull;

/**
 * @author Demon
 */
@Data
public class CommandConvert {
	/**
	 * 指令打包
	 *
	 * @param f    指令实体
	 * @param uuid 屏幕uuid
	 * @return 打包后指令
	 **/
	public static MessageLite convert(ScheduleActions f, String uuid) {
		ProtocolType protocolType = f.getProtocolType();
		String thisName = protocolType.getThisName();
		ProtocolSummationType.NameStruct nameStruct = ProtocolSummationType.NameStruct.valueOf(thisName);
		return convertMsg(f.objectConvertBytes(), uuid, nameStruct);
	}
	
	/**
	 * 指令打包
	 *
	 * @param f     指令实体
	 * @param uuid  屏幕uuid
	 * @param seqId 指令id
	 * @return 打包后指令
	 **/
	public static MessageLite convert(ScheduleActions f, String uuid, Long seqId) {
		ProtocolType protocolType = f.getProtocolType();
		String thisName = protocolType.getThisName();
		ProtocolSummationType.NameStruct nameStruct = ProtocolSummationType.NameStruct.valueOf(thisName);
		return convertMsg(f.objectConvertBytes(), uuid, seqId, nameStruct);
	}
	
	/**
	 * 更新版本需要变化
	 */
	public final static String VERSION = "1.0.1";
	
	
	public static MessageLite convertMsg(@NonNull ByteString bytes, String uuid, ProtocolSummationType.NameStruct nameStruct) {
		long seqId = System.currentTimeMillis();
		return convertMsg(bytes, uuid, seqId, nameStruct);
	}
	
	public static MessageLite convertMsg(@NonNull ByteString bytes, String uuid, Long seqId, ProtocolSummationType.NameStruct nameStruct) {
		return Gateway.Msg.newBuilder()
				.setSeqId(seqId)
				.setTimestamp(System.currentTimeMillis())
				.setBizContent(bytes)
				.setUuid(uuid)
				.setVersion(VERSION)
				.setNameStruct(nameStruct)
				.build();
	}
}
